package org.NooLab.itexx.browser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;





public class ITexxWebBrowser extends JPanel{
	
	private static final long serialVersionUID = -1910599916919632299L;

	
	ITexxWebBrowser itexxBrowser ;
	JPanel webBrowserPanel ;


	private int requestCode;
	
	
	// ========================================================================
	public ITexxWebBrowser(){
		super(new BorderLayout());
		itexxBrowser = this;
		
	}

	public ITexxWebBrowser( int internal){
		super(new BorderLayout());
		
		requestCode = internal;
		itexxBrowser = this;
		
		if (internal>=2){
			prepare();
		}
		
		// webBrowserPanel = new JPanel(new BorderLayout());
		// complete(webBrowserPanel);
	}
	// ========================================================================
	
	private void prepare(){
		
	    UIUtils.setPreferredLookAndFeel();
	    NativeInterface.open();
	    SwingUtilities.invokeLater(new Runnable() {
	      public void run() {
	        JFrame frame = new JFrame("DJ Native Swing Test");
	        
	        
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        frame.getContentPane().add(new ITexxWebBrowser(1), BorderLayout.CENTER);
	        frame.setSize(800, 600);
	        frame.setLocationByPlatform(true);
	        if (requestCode>=3){
	        	frame.setVisible(false);
	        }else{
	        	frame.setVisible(false);
	        }
	      }
	    });
	    
		// NativeInterface.open();
	    NativeInterface.runEventPump();
	
	}
	
	
	private void complete( JPanel webBrowserPanel ){
		
	    // JPanel webBrowserPanel = new JPanel(new BorderLayout());
	    webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
	    
	    final JWebBrowser webBrowser = new JWebBrowser();
	    
	    
	    
	    
	    if (requestCode==0){ // 1== standalone, 0==inside ITexxWindow == without browser decoration
	    	/*
	    	 * in ITexx we show only pre-processed HTML
	    	 */
	    	webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
	    	// webBrowserPanel.add( new DeferredDestruction(), BorderLayout.CENTER);
	    	
	    	add(webBrowserPanel, BorderLayout.CENTER);
	    	
	    	webBrowser.setMenuBarVisible(false);
	    	
	    	webBrowser.setButtonBarVisible(false);
	    	webBrowser.setLocationBarVisible(false) ;
	    	
	    }else{
	    	
	    	webBrowser.navigate("http://www.google.com");
	    	
	    	webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
	    	add(webBrowserPanel, BorderLayout.CENTER);

		    // Create an additional bar allowing to show/hide the menu bar of the web browser.
		    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
		    JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());

		    menuBarCheckBox.addItemListener(new ItemListener() {
			      public void itemStateChanged(ItemEvent e) {
			        webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
			      }
			    });
			    
			buttonPanel.add(menuBarCheckBox);
			add(buttonPanel, BorderLayout.SOUTH);
			webBrowserPanel.setVisible(true);    
	    }
	    
	    
	    

	}

	public void createBasicBrowser(final JPanel swingPanel){

		
	    UIUtils.setPreferredLookAndFeel();
	    NativeInterface.open();
	    // SwingUtilities.invokeLater(
	    
	    
	    	// this should be put into its own thread
	        // JFrame frame = new JFrame("DJ Native Swing Test");
	        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    	swingPanel.add(itexxBrowser, BorderLayout.CENTER);
	     // frame.setSize(800, 600);
	     // frame.setLocationByPlatform(true);
	     // frame.setVisible(true);
	        SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	            	complete(swingPanel);
	            }
	          });

	    	
	    	
	    NativeInterface.runEventPump();

	    System.out.println("browser has been created...") ;
	    
	}

	public static void main(String[] args) {
		
		new ITexxWebBrowser(2);
		
	}
	
	
}
